package com.yang.library.service.service.impl;


import com.chen.util.LocalDateTimeUtil;
import com.yang.library.entry.Book;
import com.yang.library.service.mapper.FileMapper;
import com.yang.library.service.mapper.impl.FileMapperImpl;
import com.yang.library.service.service.BookService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService {
    private final FileMapper fileMapper = new FileMapperImpl();

    @Override
    public boolean addBook(Book book) {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        if (bookMap.containsKey(book.getTitle())) {
            return false;
        }
        bookMap.put(book.getTitle(), book);
        fileMapper.saveBookData(bookMap);
        return true;
    }

    @Override
    public boolean removeBookByTitle(String title) {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        if (!bookMap.containsKey(title) || bookMap.get(title).isBorrowed()) {
            return false;
        }
        bookMap.remove(title);
        fileMapper.saveBookData(bookMap);
        return true;
    }

    @Override
    public boolean updateBook(String title, Book book) {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        if (bookMap.containsKey(title)) {
            if (bookMap.get(title).isBorrowed()) {
                return false;
            }
            Book book1 = bookMap.get(title);
            book1.setAuthor(book.getAuthor());
            book1.setIsbn(book.getIsbn());
            bookMap.put(title, book1);
            fileMapper.saveBookData(bookMap);
            return true;
        }
        return false;
    }

    @Override
    public List<Book> getAllBorrowedBooks() {
        List<Book> allBooks = getAllBooks();
        if (allBooks != null) {
            return allBooks.stream().filter(Book::isBorrowed).toList();
        }
        return List.of();
    }

    @Override
    public boolean borrowBook(String title, String userName) {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        if (!bookMap.containsKey(title)) {
            return false;
        }
        // 判断用户是否已经借过五本图书
        if (getBorrowedBooksByUser(userName).size() > 5) {
            return false;
        }
        Book book = bookMap.get(title);
        if (!book.isBorrowed()) {
            book.setBorrowed(true);
            book.setBorrowDate(LocalDateTimeUtil.getDate());
            book.setBorrower(userName);
            book.setReturner(null);
            book.setReturnDate(null);
            bookMap.put(title, book);
            return fileMapper.saveBookData(bookMap);
        }
        return false;
    }

    @Override
    public boolean returnBook(String title, String userName) {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        if (!bookMap.containsKey(title)) {
            return false;
        }
        Book book = bookMap.get(title);
        if (book.isBorrowed() && book.getBorrower().equals(userName)) {
            book.setBorrowed(false);
            book.setReturnDate(LocalDateTimeUtil.getDate());
            book.setReturner(userName);
            bookMap.put(title, book);
            return fileMapper.saveBookData(bookMap);
        }
        return false;
    }

    @Override
    public List<Book> getBorrowedBooksByUser(String userName) {
        return getAllBorrowedBooks().stream()
                .filter(book -> book.getBorrower().equals(userName)).toList();
    }

    @Override
    public List<Book> getBookByTitleOrAuthor(String title) {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        List<Book> bookList = new ArrayList<>();
        for (Book book : bookMap.values()) {
            if (book.getTitle().contains(title) || book.getAuthor().contains(title)) {
                bookList.add(book);
            }
        }
        return bookList;
    }

    @Override
    public List<Book> getAllBooks() {
        Map<String, Book> bookMap = new HashMap<>(fileMapper.loadBookData());
        return bookMap.values().stream().toList();
    }
}
